import httpFetch from 'share/httpFetch'
import {message} from "antd/lib/index";

const accountSettingsServer = (context) => {
  return {
    // 获取环境
    async getCloudDev(){
      try {
        context.setState({loading: true});
        const res = await httpFetch.get('/api/vendors/service/getEnv');
        return res;
      }catch (e) {
        dealWithErrorMes(e);
      }finally {
        context.setState({loading: false});
      }
    },
    // 获取线下消费商设置数据
    async getAccountSettingDefault(params) {
      try {
        context.setState({loading: true});
        const response = await httpFetch.post('/vendor-data-service/api/order/invoicePushConfig/search', params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({loading: false});
      }
    },

    // 保存线下消费商数据设置
    async saveAccountSettingDefault(params) {
      try {
        context.setState({loading: true});
        const response = await httpFetch.post('/vendor-data-service/api/order/invoicePushConfig/save', params);
        return response;
      } catch (e) {
        dealWithErrorMes(e);
      } finally {
        context.setState({loading: false});
      }
    },

    // 获取账套费用
    async getExpense(id) {
      const params = {
        setOfBooksId: id,
        supplierType: 0,
        createdManually: false,
        subsidyType: 0,
        page: 0,
        size: 1000
      }
      try {
        const res = await httpFetch.get('/api/expense/type/by/setOfBooks', params);
        return res;
      } catch (e) {
        dealWithErrorMes(e)
      }
    },

    // 获取分发确认设置
    async getSettingsData(params) {
      context.setState({loading: true});
      try {
        const response = await httpFetch.get('/vendor-info-service/company/dispenseConfig/search', params);
        return response;
      } catch (e) {
        dealWithErrorMes(e)
      } finally {
        context.setState({loading: false});
      }
    },

    // 保存分发确认
    async saveSettingsData(params) {
      context.setState({loading: true});
      try {
        const response = await httpFetch.post('/vendor-info-service/company/dispenseConfig/save', params);
        return response;
      } catch (e) {
        dealWithErrorMes(e)
      } finally {
        context.setState({loading: false});
      }
    },

    // 根据租户ID 获取租户下的所有公司
    async getCompanies() {
      context.setState({loading: true});
      try {
        const response = await httpFetch.get('/api/company/available', {page: 0, size: 1000});
        return response;
      } catch (e) {
        dealWithErrorMes(e)
      } finally {
        context.setState({loading: false})
      }
    }
  }
}

const dealWithErrorMes = (err) => {
  message.error(`error:${err.response.data.message}`);
}

export default accountSettingsServer;
